package com.work.helper.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author gosn
 */
@Data
public class Result<T> implements Serializable {
    /**状态码*/
    private Integer code;

    /**消息*/
    private String message;

    /**响应数据*/
    private T data;

    private Long timestamp;

    public Result(ResultCode resultCode, T data) {
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public Result(ResultCode resultCode) {
        this.code = resultCode.code();
        this.message = resultCode.message();
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 返回成功
     * @return
     */
    public static Result success(){
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 返回成功
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data){
        return new Result(ResultCode.SUCCESS, data);
    }
    /**
     * 返回失败
     * @return
     */
    public static Result failure(ResultCode resultCode){
        return new Result(resultCode);
    }
    /**
     * 返回失败
     * @param resultCode
     * @param data
     * @return
     */
    public static <T> Result<T> failure(ResultCode resultCode, T data){
        return new Result(resultCode, data);
    }
}
